/**
* @Title: insurance_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('InsuranceCtrl', ['$scope','$http','$state','$modal','BaseURL','Modal','InsuranceService','CommunityService',
	                                function($scope,$http,$state,$modal,baseUrl,Modal,InsuranceService,CommunityService){

         $scope.communityService = CommunityService;
         CommunityService.getBaseCode('{codeType:\'provincialDep\'}');
		CommunityService.initCodeListDatas();

       $scope.columns = [
        {label:'姓名',map:'name',width:'12%',cellTemplate:'<a ng-click="queryDetail(data.insuranceId,data.type);" ng-bind="data.name"></a>'},
        {label:'机构名称',map:'healthProviderName',width:'16%'},
        {label:'机构编号',map:'healthProviderCode',width:'12%'},
        {label:'人员分类',map:'personType',width:'12%'},
        {label:'省科室',map:'departName',width:'12%'},
        {label:'手机号码',map:'mobilePhone',width:'12%'},
        {label:'电话号码',map:'contactPhone',width:'12%'},
        {label:'变更记录',width:'12%',
            cellTemplate:'<td><a ng-click="queryInsuranceModal(data.insuranceId)">变更记录</a></td>'
        }

    ];
         $scope.currentPage = 1;
		//每页显示数
        var start = 0,
            limit = 20;
		
		//机构
		function initMedicalOrgInfoList (){
			InsuranceService.initMedicalOrgInfoList($scope.form.orgType,$scope.form.orgLevel).success(function(response){
                $scope.medicalOrgInfoList = response.medicalOrgList;
			});
		}
		//监听机构分类值
        $scope.$watch('form.orgType',function(){
			initMedicalOrgInfoList();
		});
		//监听机构等级
      $scope.$watch('form.orgLevel',function(){
			initMedicalOrgInfoList();
		});
        $scope.form = {
            page:"0",
            pageSize:"20"
        };

		//查询
      /*  $scope.$on('pageTool.skipPage',function(e,v){

            start = limit*(v-1);
            $scope.form.page = start;
            $scope.onSubmit();
        });*/
        
        $scope.queryDetail = function(doctorId,type){
    		$state.go('insurance.detail',{
    			doctorId:doctorId,
    			type:type
    		});
        };
        $scope.onSubmit = function(page){
        	if(!page){
        		page=1;
        	}
        	  $scope.currentPage = page;
            $scope.form.page=0;
            if(angular.isUndefined($scope.currentPage) == false && $scope.currentPage > 1){
                $scope.form.page = ($scope.currentPage - 1) * 20;
            }
            InsuranceService.query($scope.form).success(function(res){
                $scope.tableData = res.root;
                $scope.totalCount = res.total;
                $scope.authority = res.authority;
            });
        };
            //分页监听
            $scope.$on('pageTool.skipPage',function(e,v){
              
                $scope.onSubmit(v);
            });
         //重置
        $scope.resetBut = function(){
        	   $scope.form={};
               $scope.form = {
                       page:"0",
                       pageSize:"20"
                   };
		}
		
        $scope.onExport=function(){
            var params = $scope.form;
            var exportTotalCount = 0;
            $http({
                method:'post',
                cache: false,
                url:  baseUrl+'/hcpms/provider/findAttendantInfoCount.do',
                params:params
            }).success(function(response){
                exportTotalCount = response.total;
                if(exportTotalCount && exportTotalCount > 30000){
                    Modal.alert("数据量超过3万条不能导出，请重新筛选！");
                    return;
                }else if(exportTotalCount==0)
                {
                    Modal.alert("数据量为0条不能导出，请重新筛选！");
                    return;
                }else
                {
                    var url = baseUrl+"/hcpms/provider/exportInsurance.do?"+jQuery.param(params);
                    window.location.href = url;
                }

            });

        }
        
        /**
         * 机构详细信息--新增
         * @param params
         */
		//打开日期Model
		$scope.addDoctor = function () {
			$state.go('insurance.detail');
		};
        $scope.authority={
        		add:1,
        		update:1,
        		deleted:1
        }
        /**
         *医保人员--删除
         * @param params
         */
        $scope.checkRows=[];
        
        function findCheck(d) {
            for(var i=0;i< $scope.checkRows .length;i++) {
                var checkedData =  $scope.checkRows [i];
                if(checkedData.itemId == d.itemId) {
                    return i;
                }
            }
            return -1;
        };
        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                $scope.checkRows .push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                $scope.checkRows.splice(index,1);
            }
        });

        $scope.deleteDoctor = function(){
            var	checkRows =  $scope.checkRows;
            if (checkRows.length == 0) {
                Modal.alert("请勾选删除项！");
                return;
            }else
            {
                var ids = '';
                angular.forEach(checkRows,function(totalCheck,i){
                    ids+="'"+totalCheck.insuranceId+"',";
                });
                var  params= {
                ids : ids
                }
                Modal.confirm('确认删除吗？',function() {
                    $http.post(baseUrl + '/hcp/hcp/insurance/delete.do', params).success(function (data) {
                        if (data.state == '0') {
                            Modal.alert(data.msg);
                        } else {
                            Modal.alert("删除失败！");
                        }
                        $scope.onSubmit();
                    })
                })
            }
        };

		//查看医保服务人员信息
		$scope.queryInsuranceModal = function(insuranceId){
			var modalInstance = $modal.open({
      	      templateUrl:'insurance_modal.html',
      	      controller: function($scope, $http,$modalInstance,Modal){
		      	      //表头
		      	    $scope.modalColumns = [{label:'序号',map:'$index',width:'8%',cellTemplate:'<span ng-bind="$index+1"></span>'},
		      	                           {label:'变更内容',map:'changeContent',width:'17%'},
			      	                       {label:'变更前',map:'changeBefore',width:'30%'},
			      	                       {label:'变更后',map:'changeAfter',width:'30%'},
			      	                       {label:'生效日期',map:'applicantionDate',width:'15%'}
		      	                           ];
                  $http({
                      method : 'GET',
                      cache : false,
                      url : baseUrl + '/hcpms/attendant/findHistoryChangeRecord.do',
                      params : {
                          relationId : insuranceId,
                          changeType:'1'
                      }
                  }).success(function(response, status, headers, config) {
                      $scope.modalTableData = response.result;
                  });

      	    	 //取消
      	    	  $scope.onOk = function() {
					  $modalInstance.dismiss();
				  }
      	      }
	      	});
			modalInstance.result.then(function(item) {
				
	  	    });
		}
         $scope.onSubmit();
	}]);
})();